package enterprise.web.tookit.scheduling;

import java.util.Date;

import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;

import org.apache.log4j.Logger;

@Stateless(mappedName = "TaskScheduler")
public class TaskSchedulerImpl implements TaskScheduler {

	@Resource
	private TimerService timerService;

	/**
	 * log
	 */
	private static Logger log = Logger.getLogger(TaskSchedulerImpl.class);

	/**
	 * Default constructor.
	 */
	public TaskSchedulerImpl() {
	}

	@Timeout
	public void scheduleTask(final Timer timer) {
		log.info(timer.getInfo());
		System.out.println("TIMER");
	}

	@Override
	public void cancelScheduling() {
		for (Object currentTimer : timerService.getTimers()) {
			Timer timer = (Timer) currentTimer;
			log.info("Cancel Timer ...." + timer.getInfo());
			timer.cancel();
		}
	}

	@Override
	public void createTimer() {
		timerService.createTimer(new Date(System.currentTimeMillis() + 100000), "My first Timer service....");

	}

}